/**
 * All rights reserved by www.xmerge.cn
 */
package net.xfok.xmerge.manage;

import net.xfok.xmerge.R;
import net.xfok.xmerge.provider.RssItem;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.util.Log;
import android.widget.Toast;

/**
 * @author YangHua
 *
 */
public class RssManage {
	
	public static boolean checkNet(Context context){
		boolean result=false;
		ConnectivityManager cm=(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo netinfo = cm.getActiveNetworkInfo();
		if ( netinfo !=null&&netinfo.isConnected() ) {
			result=true;
			Log.i( context.getClass().getName(), "The net was connected" );
		}else{
			Toast.makeText(context,context.getResources().getText(R.string.dialog_message_net_not), Toast.LENGTH_LONG).show();		
		}
		return result;
	}
	
	 public static void addRssItem(RssItem rss,Context context) {
		 ContentValues values = new ContentValues();
		 values.put(RssItem.Rss.RSS_COLUMN_NAME, rss.getName());
		 values.put(RssItem.Rss.RSS_COLUMN_URL, rss.getUrl());
		 values.put(RssItem.Rss.RSS_COLUMN_CHARSET, rss.getCharset());
		 context.getContentResolver().insert(RssItem.Rss.CONTENT_URI, values);
   	 } 
	 
	 public static void delRssItem(RssItem rss,Context context){
    	 Log.i(context.getClass().getName(), "_id="+rss.getId());
    	 context.getContentResolver().delete(RssItem.Rss.CONTENT_URI, RssItem.Rss.RSS_COLUMN_ID+"="+rss.getId(), null);
     }
	 
	 public static RssItem getRssItem(Cursor cur){
    	 RssItem rssItem=null;
    	 if(cur!=null){
	    	 int id = cur.getInt(cur.getColumnIndex(RssItem.Rss.RSS_COLUMN_ID));
	         String charset=cur.getString(cur.getColumnIndex(RssItem.Rss.RSS_COLUMN_CHARSET));
	         String name = cur.getString(cur.getColumnIndex(RssItem.Rss.RSS_COLUMN_NAME));
	         String url=cur.getString(cur.getColumnIndex(RssItem.Rss.RSS_COLUMN_URL));
	         rssItem=new RssItem(id,charset,name,url);
    	 }
         return rssItem;
     }
	
}
